
local ServerOHOH = _G.ServerOHOH;

local levels_cfg = ServerOHOH.config.Levels
local game_cfg = ServerOHOH.config.Game
local FirendListHelper=_G.CommonOHOH.load.FirendListHelper()

local M={}

local PlayerFirendsBuff={}

function IsValid(userId,data,lid)
    if data[lid] and data[lid].userId==userId then
        return false
    end

    for k,v in pairs(data) do
        if v.userId==userId and math.abs(k-lid)<game_cfg.MaxLevelSameUserId then
            return false
        end
    end

    return true
end

function FindOneFriend(lid,player,data)

    -- 优先选取同房间好友
    for _,v in pairs(game.Players:GetPlayers()) do
        --不是自己，并且是好友
        if v.UserId~=player.UserId and player:IsFriendsWith(v.UserId) and IsValid(v.UserId,data,lid)then
           return v.UserId
        end
    end

    -- 不存在好友缓存,创建
    if not PlayerFirendsBuff[player.UserId] then
        PlayerFirendsBuff[player.UserId]=FirendListHelper.CreateFriendBuffer(player)
    end

    --在线好友中选
    for _,v in pairs(PlayerFirendsBuff[player.UserId]:GetOnLineFriend()) do
        if  IsValid(v.UserId,data,lid)then
           return v.UserId
        end
    end
    
    --离线好友中选
    for _,v in pairs(PlayerFirendsBuff[player.UserId]:GetOfflineFirend()) do
        if  IsValid(v.UserId,data,lid)then
           return v.UserId
        end
    end

    --同房间中选
    for _,v in pairs(game.Players:GetPlayers()) do
        if v.UserId~=player.UserId and IsValid(v.UserId,data,lid) then
           return v.UserId
        end
    end

    -- 没有合法的人
    return nil
end

function FullOneLevel(lid,uid,data)
    if data[lid] then
        local ouid=data[lid].userId
        local ocount=data[lid].count
        if data[lid].statistics then
            if  data[lid].statistics[ouid] then
                data[lid].statistics[ouid]=data[lid].statistics[ouid]+ocount
            else
                data[lid].statistics[ouid]=ocount
            end
        else
            data[lid]["statistics"]={}
            data[lid].statistics[ouid]=ocount
        end
        data[lid].userId=uid
        data[lid].count=0
    else
        data[lid]={
            userId=uid,
            count=0,
            statistics={
            }
        }
    end
    return data
end




function M.InitData(self,player,data)
    local level={}
    for i=1,game_cfg.InitLevelDataCount do
        table.insert(level,i)
    end
    return self:CreateLevelData(player,data,level)
end
--Player（系统),data(关卡数据),level需要生成的关卡列表
function M.CreateLevelData(self,player,data,level)

    for _,lid in pairs(level) do
        assert(levels_cfg[lid],"No Level:"..lid)
        if levels_cfg[lid].isFriend==1 then
            local uid=FindOneFriend(lid,player,data)
            if not uid then
                uid=levels_cfg[lid].rescueUserId 
            end
            FullOneLevel(lid,uid,data)
        else
            FullOneLevel(lid,levels_cfg[lid].rescueUserId,data)
        end
    end

    return data

end

-- 通关了某关
function M.PassLevel(self,player,data,lid,saveBack)
    data[lid].count=data[lid].count+1
    if data[lid].count>=levels_cfg[lid].needSave then
        if saveBack then
            saveBack(lid,data[lid].userId,data[lid].count)
        end
        local d=self:CreateLevelData(player,data,{lid})
        -- ServerOHOH:PrintTable(d)
        return d
    end
    -- ServerOHOH:PrintTable(data)
    return data
end


function M.UnLockLevelBuffer(self,player,data,lid)
    local maxId=lid --+game_cfg.InitLevelDataCount
    if levels_cfg[maxId] and not data[maxId] then
        return self:CreateLevelData(player,data,{maxId})
    end
end

return M